package com.turnengine.client.local.alliance.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.alliance.bean.IAlliance;
import com.turnengine.client.local.alliance.command.DisbandAlliance;
import com.turnengine.client.local.alliance.command.FormAlliance;
import com.turnengine.client.local.alliance.command.GetAllianceById;
import com.turnengine.client.local.alliance.command.GetAllianceByName;
import com.turnengine.client.local.alliance.command.GetAllianceCount;
import com.turnengine.client.local.alliance.command.GetAllianceInviteLimit;
import com.turnengine.client.local.alliance.command.GetAllianceList;
import com.turnengine.client.local.alliance.command.GetAllianceMemberCount;
import com.turnengine.client.local.alliance.command.GetAllianceMemberLimit;
import com.turnengine.client.local.alliance.command.GetInvitedToJoinAllianceList;
import com.turnengine.client.local.alliance.command.IDisbandAlliance;
import com.turnengine.client.local.alliance.command.IFormAlliance;
import com.turnengine.client.local.alliance.command.IGetAllianceById;
import com.turnengine.client.local.alliance.command.IGetAllianceByName;
import com.turnengine.client.local.alliance.command.IGetAllianceCount;
import com.turnengine.client.local.alliance.command.IGetAllianceInviteLimit;
import com.turnengine.client.local.alliance.command.IGetAllianceList;
import com.turnengine.client.local.alliance.command.IGetAllianceMemberCount;
import com.turnengine.client.local.alliance.command.IGetAllianceMemberLimit;
import com.turnengine.client.local.alliance.command.IGetInvitedToJoinAllianceList;
import com.turnengine.client.local.alliance.command.IInviteToJoinAlliance;
import com.turnengine.client.local.alliance.command.IKickAllianceMember;
import com.turnengine.client.local.alliance.command.ILeaveAlliance;
import com.turnengine.client.local.alliance.command.IRequestToJoinAlliance;
import com.turnengine.client.local.alliance.command.ISetAllianceInviteLimit;
import com.turnengine.client.local.alliance.command.ISetAllianceMemberLimit;
import com.turnengine.client.local.alliance.command.ISetAlliancesEnabled;
import com.turnengine.client.local.alliance.command.InviteToJoinAlliance;
import com.turnengine.client.local.alliance.command.KickAllianceMember;
import com.turnengine.client.local.alliance.command.LeaveAlliance;
import com.turnengine.client.local.alliance.command.RequestToJoinAlliance;
import com.turnengine.client.local.alliance.command.SetAllianceInviteLimit;
import com.turnengine.client.local.alliance.command.SetAllianceMemberLimit;
import com.turnengine.client.local.alliance.command.SetAlliancesEnabled;
import java.util.List;

/**
 * The Local Alliance Command Set.
 */
public class LocalAllianceCommandSet implements ILocalAllianceCommandSet {

	@Override
	public int getAllianceCount(long loginId, int instanceId) {
		IGetAllianceCount command = new GetAllianceCount(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getAllianceMemberCount(long loginId, int instanceId, int id) {
		IGetAllianceMemberCount command = new GetAllianceMemberCount(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IAlliance getAllianceById(long loginId, int instanceId, int id) {
		IGetAllianceById command = new GetAllianceById(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IAlliance> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IAlliance getAllianceByName(long loginId, int instanceId, String name) {
		IGetAllianceByName command = new GetAllianceByName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IAlliance> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IAlliance formAlliance(long loginId, int instanceId, String name) {
		IFormAlliance command = new FormAlliance(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IAlliance> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean disbandAlliance(long loginId, int instanceId) {
		IDisbandAlliance command = new DisbandAlliance(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean leaveAlliance(long loginId, int instanceId) {
		ILeaveAlliance command = new LeaveAlliance(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean inviteToJoinAlliance(long loginId, int instanceId, int playerId) {
		IInviteToJoinAlliance command = new InviteToJoinAlliance(loginId, instanceId, playerId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean requestToJoinAlliance(long loginId, int instanceId, int allianceId) {
		IRequestToJoinAlliance command = new RequestToJoinAlliance(loginId, instanceId, allianceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean kickAllianceMember(long loginId, int instanceId, int playerId) {
		IKickAllianceMember command = new KickAllianceMember(loginId, instanceId, playerId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<Integer> getInvitedToJoinAllianceList(long loginId, int instanceId, int allianceId) {
		IGetInvitedToJoinAllianceList command = new GetInvitedToJoinAllianceList(loginId, instanceId, allianceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<Integer>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getAllianceInviteLimit(long loginId, int instanceId) {
		IGetAllianceInviteLimit command = new GetAllianceInviteLimit(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int setAllianceInviteLimit(long loginId, int instanceId, int limit) {
		ISetAllianceInviteLimit command = new SetAllianceInviteLimit(loginId, instanceId, limit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getAllianceMemberLimit(long loginId, int instanceId) {
		IGetAllianceMemberLimit command = new GetAllianceMemberLimit(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int setAllianceMemberLimit(long loginId, int instanceId, int limit) {
		ISetAllianceMemberLimit command = new SetAllianceMemberLimit(loginId, instanceId, limit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setAlliancesEnabled(long loginId, int instanceId, boolean enabled) {
		ISetAlliancesEnabled command = new SetAlliancesEnabled(loginId, instanceId, enabled);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IAlliance> getAllianceList(long loginId, int instanceId) {
		IGetAllianceList command = new GetAllianceList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IAlliance>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
